package com.limit.config;

import java.io.File;
import java.util.List;
import java.util.Properties;

import net.sf.ehcache.CacheManager;

import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.render.ViewType;
import com.limit.cache.LoadService;
import com.limit.constant.Constant;
import com.limit.control.admin.ActorController;
import com.limit.control.admin.AdSlideController;
import com.limit.control.admin.AdminController;
import com.limit.control.admin.AjaxController;
import com.limit.control.admin.DownIncomeController;
import com.limit.control.admin.GameCommentController;
import com.limit.control.admin.GameController;
import com.limit.control.admin.GameTypeController;
import com.limit.control.admin.IndexController;
import com.limit.control.admin.LoginController;
import com.limit.control.admin.ModuleController;
import com.limit.control.admin.RankController;
import com.limit.control.site.AjaxSiteController;
import com.limit.control.site.DetailController;
import com.limit.control.site.DownController;
import com.limit.control.site.IndexSiteController;
import com.limit.model.Actor;
import com.limit.model.ActorModule;
import com.limit.model.AdSlide;
import com.limit.model.Admin;
import com.limit.model.Game;
import com.limit.model.GameComment;
import com.limit.model.GameDownRecord;
import com.limit.model.GameType;
import com.limit.model.Module;
import com.limit.model.Rank;
import com.limit.model.RankGame;
import com.limit.utils.PropertiesUtil;

public class GlobalConfig extends JFinalConfig {

	/**
	 * 常量设置
	 */
	@Override
	public void configConstant(Constants me) {
		me.setDevMode(true);
		me.setError404View("404.html");
		me.setEncoding("utf-8");
		me.setViewType(ViewType.FREE_MARKER);
	}

	/**
	 * 配置路由,将请求映射到控制器
	 */
	@Override
	public void configRoute(Routes me) {
		//前台
		me.add("/", IndexSiteController.class,"/");
		me.add("/detail",DetailController.class,"/");
		me.add("/app/down",DownController.class,"/");
		//后台
		me.add("/login", LoginController.class,"/admin/");
		me.add("/admin", IndexController.class,"/admin/iframe/"); 
		me.add("/admin/slide", AdSlideController.class ,"/admin/");
		me.add("/admin/actor", ActorController.class ,"/admin/");
		me.add("/admin/admin", AdminController.class, "/admin/");
		me.add("/admin/module", ModuleController.class, "/admin/");
		me.add("/admin/game", GameController.class ,"/admin/");
		me.add("/admin/gametype", GameTypeController.class ,"/admin/");
		me.add("/admin/gamecomment", GameCommentController.class ,"/admin/");
		me.add("/admin/downincome", DownIncomeController.class ,"/admin/");
		me.add("/admin/rank", RankController.class ,"/admin/");
		//Ajax
		me.add("/admin/ajax", AjaxController.class);
		me.add("/site/ajax", AjaxSiteController.class);
		
	}

	/**
	 * 配置插件,选择数据库,参数配置
	 */
	@Override
	public void configPlugin(Plugins me) {
		Properties properties = loadPropertyFile("config" + File.separator + "c3p0.properties");
		C3p0Plugin c3p0Plugin = new C3p0Plugin(properties);
		me.add(c3p0Plugin);
		ActiveRecordPlugin active = new ActiveRecordPlugin(c3p0Plugin);
		me.add(active);
		active.setShowSql(true);
		active.addMapping("actor", "id", Actor.class);
		active.addMapping("ad_slide", "id", AdSlide.class);
		active.addMapping("admin", "id", Admin.class);
		active.addMapping("game", "id", Game.class);
		active.addMapping("game_comment", "id", GameComment.class);
		active.addMapping("game_down_record", "id", GameDownRecord.class);
		active.addMapping("game_type", "id", GameType.class);
//		active.addMapping("web_config", "id", WebConfig.class);
		active.addMapping("actor_module", "id", ActorModule.class);
		active.addMapping("module", "id", Module.class);
		active.addMapping("rank", "id", Rank.class);
		active.addMapping("rank_game", "id", RankGame.class);
		
		me.add(new EhCachePlugin());  //开启  EhCache
		
		
	}

	@Override
	public void configInterceptor(Interceptors me) {
		
	}

	@Override
	public void configHandler(Handlers me) {
		
	}

	/**
	 * 预加载
	 */
	@Override
	public void afterJFinalStart() {
//		LoadService.loadAdminActor();
//		LoadService.loadGameTypes();
	}

	@Override
	public void beforeJFinalStop() {
		CacheKit.remove(Constant.CacheActorPermission, Constant.CacheActorPermission);
		CacheKit.remove(Constant.CacheGameTypes, Constant.CacheGameTypes);
		CacheKit.remove(Constant.CacheActor, Constant.CacheActor);
		super.beforeJFinalStop();
	}

	
}
